package fr.umlv.metadata;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.ResourceBundle;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.text.JTextComponent;

import org.alfresco.cmis.client.AlfrescoDocument;
import org.apache.chemistry.opencmis.client.api.CmisObject;
import org.apache.chemistry.opencmis.client.api.Property;
import org.apache.chemistry.opencmis.commons.definitions.PropertyDefinition;
import org.apache.chemistry.opencmis.commons.enums.PropertyType;

import fr.umlv.authentication.SessionHandler;


// TODO: Auto-generated Javadoc
/**
 * The Class AspectPanel.
 */
public class AspectPanel extends JPanel{

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;
	
	/** The aspect utils. */
	private AspectUtils aspectUtils;
	
	/** The name. */
	private String name;
	
	/** The bundle. */
	private ResourceBundle bundle;
	
	/** The tabbed pane. */
	private JTabbedPane tabbedPane;
	
	/** The all aspects. */
	private HashSet<String> allAspects ; 
	
	/** The h field. */
	private int hField = 20;
	
	/** The w field. */
	private int wField = 32;

	/** The map to check type. */
	private Map<JTextComponent,PropertyDefinition<?>> mapToCheckType;

	/**
	 * Creates the tab.
	 *
	 * @param nameTab the name tab
	 */
	public void createTab(final String nameTab) {

		if(!allAspects.contains(nameTab)) {
			
			allAspects.add(nameTab);
			if(!tabbedPane.isVisible()) tabbedPane.setVisible(true);
			final JPanel ptab = new JPanel();
			ptab.setLayout(new BorderLayout());
			
			final List<JTextComponent> listField = new ArrayList<JTextComponent>();

			Box boxPanel = Box.createVerticalBox();
			boxPanel.setAlignmentX(0);
			
			for(PropertyDefinition<?> prop : aspectUtils.getPropertiesAsList(nameTab)){
				
				JPanel panel = new JPanel(new FlowLayout(0));
				if(PropertyType.BOOLEAN.equals(prop.getPropertyType())){

					JCheckBox box = new JCheckBox(prop.getDisplayName());
					box.setName(nameTab+prop.getDisplayName()+name);
					panel.add(box);

				}else if(PropertyType.ID.equals(prop.getPropertyType())){

					JPanel line1 = new JPanel(new FlowLayout(0));
					line1.add(new JLabel(prop.getDisplayName()+" : "));
					boxPanel.add(line1);
					
					JTextArea field = new JTextArea(8,wField);
					field.setName(nameTab+prop.getDisplayName()+name);
					panel.add(new JScrollPane(field));
					mapToCheckType.put(field,prop);
					listField.add(field);

				}else {
	
					JPanel line1 = new JPanel(new FlowLayout(0));
					line1.add(new JLabel(prop.getDisplayName()+" : "));
					boxPanel.add(line1);
					JTextField field = new JTextField(wField);
					field.setName(nameTab+prop.getDisplayName()+name);
					panel.add(field);
					mapToCheckType.put(field,prop);
					listField.add(field);
				}		
				
				boxPanel.add(panel);

			}
			
			final JScrollPane scrollPane = new JScrollPane(ptab);
			//ptab.setPreferredSize(new Dimension(400, 400));
			JButton bdelete = new JButton(bundle.getString("Delete.Name"));
			bdelete.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					tabbedPane.remove(scrollPane);
					allAspects.remove(nameTab);
					for(JTextComponent field : listField){
						mapToCheckType.remove(field);
					}
					if(tabbedPane.getTabCount()==0) {
						tabbedPane.setVisible(false);
					}
					tabbedPane.revalidate();
				}
			});

			JPanel centerPanel = new JPanel(new FlowLayout());
			centerPanel.add(boxPanel);
			ptab.add(centerPanel);
			
			JPanel buttonPanel = new JPanel(new FlowLayout());
			buttonPanel.add(bdelete);
			ptab.add(buttonPanel,BorderLayout.SOUTH);
		
			JTextField aspectName = new JTextField();
			aspectName.setName(nameTab+name); 
			ptab.add(aspectName,BorderLayout.NORTH);
			aspectName.setVisible(false);
			tabbedPane.addTab(nameTab,scrollPane);

		}
	}


	/**
	 * Creates the tab.
	 *
	 * @param nameTab the name tab
	 * @param list the list
	 * @param cmisObject the cmis object
	 */
	public void createTab(final String nameTab, List<HashMap<String, Property<?>>> list,final CmisObject cmisObject) {

		if(!allAspects.contains(nameTab)) {
			allAspects.add(nameTab);
			if(!tabbedPane.isVisible()) tabbedPane.setVisible(true);
			final JPanel ptab = new JPanel();
			ptab.setLayout(new BorderLayout());

			JPanel propPanel = new JPanel();
			propPanel.setLayout(new BoxLayout(propPanel, BoxLayout.Y_AXIS));
			final List<JTextField> listField = new ArrayList<JTextField>();

			for (HashMap<String, Property<?>> map : list) {
				for (Entry<String, Property<?>> pop : map.entrySet()) {
				
					JPanel line1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
					line1.add(new JLabel(pop.getKey()+" : "));
					JPanel line2 = new JPanel(new FlowLayout(FlowLayout.LEFT));

					if(PropertyType.BOOLEAN.equals(pop.getValue().getDefinition().getPropertyType())){

						JCheckBox box = new JCheckBox(pop.getKey());


						//aba test
						//if(pop.getValue().getValueAsString().equals("true")) {
						if(Boolean.TRUE.equals(pop.getValue().getValue())) {
							box.setSelected(true);
						}else {
							box.setSelected(false);
						}

						box.setName(nameTab+pop.getKey()+name);
						line2.add(box);

					}else if(PropertyType.DATETIME.equals(pop.getValue().getDefinition().getPropertyType())){

						GregorianCalendar gcalendar = pop.getValue().getValue();
						JTextField field = new JTextField(wField);
						if(gcalendar!=null) 
							field.setText(SessionHandler.getDateFormat().format(gcalendar.getTime()));
						field.setPreferredSize(new Dimension(wField, hField));
						field.setName(nameTab+pop.getKey()+name);
						line2.add(field);
						mapToCheckType.put(field,pop.getValue().getDefinition());
						listField.add(field);
						propPanel.add(line1);

					}	
					else{

						JTextField field = new JTextField(wField);
						field.setText(pop.getValue().getValueAsString());
						field.setPreferredSize(new Dimension(wField, hField));
						field.setName(nameTab+pop.getKey()+name);
						line2.add(field);
						mapToCheckType.put(field,pop.getValue().getDefinition());
						propPanel.add(line1);
					}		

					propPanel.add(line2);
				}
			}

			JPanel centerPanel = new JPanel(new FlowLayout());
			centerPanel.add(propPanel);
			ptab.add(centerPanel);
			
			ptab.add(new JScrollPane(centerPanel));
			ptab.setPreferredSize(new Dimension(400, 400));
			JButton bdelete = new JButton(bundle.getString("Delete.Name"));
			bdelete.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					tabbedPane.remove(ptab);
					allAspects.remove(nameTab);
					
					for(JTextField field : listField){
						mapToCheckType.remove(field);
					}
					
					AlfrescoDocument doc = (AlfrescoDocument)cmisObject;
				
					if(doc.hasAspect(aspectUtils.getID(nameTab))){
						doc.removeAspect(aspectUtils.getID(nameTab));
					}
					
					if(tabbedPane.getTabCount()==0) {
						tabbedPane.setVisible(false);
					}
				}
			});


			JPanel bottomTab = new JPanel();
			bottomTab.add(bdelete);
			ptab.add(bottomTab,BorderLayout.SOUTH);
			JTextField aspectName = new JTextField();
			aspectName.setName(nameTab+name); 
			ptab.add(aspectName,BorderLayout.NORTH);
			aspectName.setVisible(false);
			tabbedPane.addTab(nameTab,ptab);

		}
	}



	//not use
	/**
	 * Display tab.
	 *
	 * @param nameTab the name tab
	 * @param keyValueProperty the key value property
	 */
	public void displayTab(final String nameTab, List<HashMap<String, String>> keyValueProperty ) {

		if(!allAspects.contains(nameTab)) {
			allAspects.add(nameTab);
			tabbedPane.setVisible(true);
			final JPanel ptab = new JPanel();
			ptab.setLayout(new BorderLayout());

			JPanel propPanel = new JPanel();
			propPanel.setLayout(new BoxLayout(propPanel, BoxLayout.Y_AXIS));

			for (HashMap<String, String> map : keyValueProperty) {
				for (Entry<String,String> emap : map.entrySet()) {
					JPanel line1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
					line1.add(new JLabel(emap.getKey()+" : "));
					JPanel line2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
					JTextField field = new JTextField(emap.getValue());
					field.setPreferredSize(new Dimension(wField, hField));
					field.setName(nameTab+emap.getKey()+name);
					line2.add(field);
					propPanel.add(line1);
					propPanel.add(line2);
				}
			}


			//aba test : must be modify
			if(keyValueProperty.size()<10) {
				for (int i = 0; i < 20; i++) {
					propPanel.add(new JLabel(" "));
				}
			}


			ptab.add(new JScrollPane(propPanel));
			ptab.setPreferredSize(new Dimension(400, 400));
			JButton bdelete = new JButton(bundle.getString("Delete.Name"));
			bdelete.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					tabbedPane.remove(ptab);
					allAspects.remove(nameTab);
					if(tabbedPane.getTabCount()==0) {
						tabbedPane.setVisible(false);
					}
				}
			});


			JPanel bottomTab = new JPanel();
			bottomTab.add(bdelete);
			ptab.add(bottomTab,BorderLayout.SOUTH);
			JTextField aspectName = new JTextField();
			aspectName.setName(nameTab+name); 
			ptab.add(aspectName,BorderLayout.NORTH);
			aspectName.setVisible(false);
			tabbedPane.addTab(nameTab,ptab);
		}
	}


	/**
	 * Instantiates a new aspect panel.
	 *
	 * @param aspectUtils the aspect utils
	 * @param name the name
	 * @param bundle the bundle
	 */
	public AspectPanel(AspectUtils aspectUtils,String name,ResourceBundle bundle) {

		this.aspectUtils = aspectUtils;
		this.name = name;
		this.bundle = bundle;
		allAspects = new HashSet<String>(); 
		mapToCheckType = new HashMap<JTextComponent,PropertyDefinition<?>>();

		setLayout(new BorderLayout());
		JPanel top = new JPanel();

		final JComboBox cb = new JComboBox(aspectUtils.getAspectNames().toArray());

		tabbedPane=new JTabbedPane();

		JButton bok = new JButton(bundle.getString("Add.Name"));
		bok.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String selecteditem = cb.getSelectedItem().toString();

				createTab(selecteditem);
			}
		});

		top.add(cb);
		top.add(bok);
		tabbedPane.setPreferredSize(new Dimension(400,400));
		tabbedPane.setVisible(false);
		add(top,BorderLayout.NORTH);
		add(tabbedPane);

	}

	/**
	 * Gets the tabbed pane.
	 *
	 * @return the tabbed pane
	 */
	public JTabbedPane getTabbedPane() {
		return tabbedPane;
	}

	/**
	 * Gets the map to check type.
	 *
	 * @return the map to check type
	 */
	public Map<JTextComponent,PropertyDefinition<?>> getMapToCheckType() {
		return mapToCheckType;
	}

}
